<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 班主任考核信息主页
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.tid" placeholder="教职工编号" class="handle-input mr10"></el-input>
                <el-input v-model="query.name" placeholder="教师姓名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>

            <div class="handle-right">
                <el-button type="primary" icon="el-icon-plus" @click="createForm">增加</el-button>
            </div>

            <el-table :data="tableData" border class="table" ref="multipleTable"
                      header-cell-class-name="table-header">
                <el-table-column type="index" label="序号" width="55" align="center" :index="indexAdd"></el-table-column>
                <el-table-column prop="name" label="教师姓名" align="center"></el-table-column>
                <el-table-column prop="tid" label="教职工编号" align="center" width="150%"></el-table-column>
                <el-table-column prop="classNo" label="所在班级" align="center" width="150%"></el-table-column>
                <el-table-column prop="amount" label="学生人数" align="center"></el-table-column>
                <el-table-column prop="awards" label="获奖情况" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="assessmentManagement.amount" label="处分人数" align="center">
                </el-table-column>
                <el-table-column prop="assessmentManagement.type" label="处分类型" align="center">
                </el-table-column>
                <el-table-column prop="assessmentManagement.attendanceFrequency" label="听课次数" align="center">
                </el-table-column>
                <el-table-column prop="assessmentManagement.classFrequency" label="班会次数" align="center">
                </el-table-column>
                <el-table-column prop="assessmentManagement.dormitoriesFrequency" label="检查宿舍次数"
                                 align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentManagement.activeFrequency" label="参加活动次数" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.failedAmount" label="学期不及格人次" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.failedPercentage" label="学期不及格百分比" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.gradeFourAmount" label="四级通过人数" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.gradeFourPercentage" label="四级通过百分比" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.gradeSixAmount" label="六级通过人数" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.gradeSixPercentage" label="六级通过百分比" align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.firstScholarshipAmount" label="一等奖学金人数"
                                 align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.firstScholarshipPercentage" label="一等奖学金百分比"
                                 align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.secondScholarshipAmount" label="二等奖学金人数"
                                 align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.secondScholarshipPercentage" label="二等奖学金百分比"
                                 align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.thirdScholarshipAmount" label="三等奖学金人数"
                                 align="center" width="150%">
                </el-table-column>
                <el-table-column prop="assessmentStudy.thirdScholarshipPercentage" label="三等奖学金百分比"
                                 align="center" width="150%">
                </el-table-column>
                <!--        <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"-->
                <!--                         width="200%"-->
                <!--        ></el-table-column>-->
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                                   @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                               :page-size="query.pageSize" :total="pageTotal"
                               @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="40%">
            <el-form ref="formRef" :model="form" :rules="editRules" label-width="150px">
                <el-form-item label="教师姓名" prop="name">
                    <el-input v-model="form.name" type="text" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="教职工编号" prop="tid">
                    <el-input v-model="form.tid" type="text" placeholder="请输入教职工编号"></el-input>
                </el-form-item>
                <el-form-item label="所在班级" prop="classNo">
                    <el-input v-model="form.classNo" type="text" placeholder="请输入班级"></el-input>
                </el-form-item>
                <el-form-item label="学生人数" prop="amount">
                    <el-input v-model="form.amount" type="text" placeholder="请输入学生人数"></el-input>
                </el-form-item>
                <el-form-item label="获奖情况" prop="awards">
                    <el-input v-model="form.awards" type="text" placeholder="请输入获奖情况"></el-input>
                </el-form-item>
                <el-form-item label="处分人数" prop="assessmentManagement.amount">
                    <el-input v-model="form.assessmentManagement.amount" type="text" placeholder="请输入处分人数"></el-input>
                </el-form-item>
                <el-form-item label="处分类型" prop="assessmentManagement.type">
                    <el-select v-model="form.assessmentManagement.type" placeholder="请选择处分类型">
                        <el-option key="警告" label="警告" value="警告"></el-option>
                        <el-option key="严重警告" label="严重警告" value="严重警告"></el-option>
                        <el-option key="记过" label="记过" value="记过"></el-option>
                        <el-option key="留校查看" label="留校查看" value="留校查看"></el-option>
                        <el-option key="勒令退学" label="勒令退学" value="勒令退学"></el-option>
                        <el-option key="开除学籍" label="开除学籍" value="开除学籍"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="听课次数" prop="assessmentManagement.attendanceFrequency">
                    <el-input v-model="form.assessmentManagement.attendanceFrequency" type="text"
                              placeholder="请输入听课次数"></el-input>
                </el-form-item>
                <el-form-item label="班会次数" prop="assessmentManagement.classFrequency">
                    <el-input v-model="form.assessmentManagement.classFrequency" type="text"
                              placeholder="请输入班会次数"></el-input>
                </el-form-item>
                <el-form-item label="检查宿舍次数" prop="assessmentManagement.dormitoriesFrequency">
                    <el-input v-model="form.assessmentManagement.dormitoriesFrequency" type="text"
                              placeholder="请输入检查宿舍次数"></el-input>
                </el-form-item>
                <el-form-item label="参加活动次数" prop="assessmentManagement.activeFrequency">
                    <el-input v-model="form.assessmentManagement.activeFrequency" type="text"
                              placeholder="请输入参加活动次数"></el-input>
                </el-form-item>
                <el-form-item label="学期不及格人次" prop="assessmentStudy.failedAmount">
                    <el-input v-model="form.assessmentStudy.failedAmount" type="text"
                              placeholder="请输入学期不及格人次"></el-input>
                </el-form-item>
                <el-form-item label="四级通过人数" prop="assessmentStudy.gradeFourAmount">
                    <el-input v-model="form.assessmentStudy.gradeFourAmount" type="text"
                              placeholder="请输入四级通过人数"></el-input>
                </el-form-item>
                <el-form-item label="六级通过人数" prop="assessmentStudy.gradeSixAmount">
                    <el-input v-model="form.assessmentStudy.gradeSixAmount" type="text" placeholder="请输入:"></el-input>
                </el-form-item>
                <el-form-item label="一等奖学金人数" prop="assessmentStudy.firstScholarshipAmount">
                    <el-input v-model="form.assessmentStudy.firstScholarshipAmount" type="text"
                              placeholder="请输入:"></el-input>
                </el-form-item>
                <el-form-item label="二等奖学金人数" prop="assessmentStudy.secondScholarshipAmount">
                    <el-input v-model="form.assessmentStudy.secondScholarshipAmount" type="text"
                              placeholder="请输入:"></el-input>
                </el-form-item>
                <el-form-item label="三等奖学金人数" prop="assessmentStudy.thirdScholarshipAmount">
                    <el-input v-model="form.assessmentStudy.thirdScholarshipAmount" type="text"
                              placeholder="请输入:"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>


        <!-- 新建弹出框 -->
        <el-dialog title="新建" v-model="addVisible" width="40%">
            <el-form ref="formRef" :model="form" :rules="editRules" label-width="150px">
                <!--        <el-form-item label="教师姓名" prop="name">-->
                <!--          <el-input  v-model="form.name" type="text" placeholder="请输入用户名" ></el-input>-->
                <!--        </el-form-item>-->
                <el-form-item label="教职工编号" prop="tid">
                    <el-input v-model="form.tid" type="text" placeholder="请输入教职工编号"></el-input>
                </el-form-item>
                <el-form-item label="所在班级" prop="classNo">
                    <el-input v-model="form.classNo" type="text" placeholder="请输入班级"></el-input>
                </el-form-item>
                <el-form-item label="学生人数" prop="amount">
                    <el-input v-model="form.amount" type="text" placeholder="请输入学生人数"></el-input>
                </el-form-item>
                <el-form-item label="获奖情况" prop="awards">
                    <el-input v-model="form.awards" type="text" placeholder="请输入获奖情况"></el-input>
                </el-form-item>
                <el-form-item label="处分人数" prop="assessmentManagement.amount">
                    <el-input v-model="form.assessmentManagement.amount" type="text" placeholder="请输入处分人数"></el-input>
                </el-form-item>
                <el-form-item label="处分类型" prop="assessmentManagement.type">
                    <el-select v-model="form.assessmentManagement.type" placeholder="请选择处分类型">
                        <el-option key="警告" label="警告" value="警告"></el-option>
                        <el-option key="严重警告" label="严重警告" value="严重警告"></el-option>
                        <el-option key="记过" label="记过" value="记过"></el-option>
                        <el-option key="留校查看" label="留校查看" value="留校查看"></el-option>
                        <el-option key="勒令退学" label="勒令退学" value="勒令退学"></el-option>
                        <el-option key="开除学籍" label="开除学籍" value="开除学籍"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="听课次数" prop="assessmentManagement.attendanceFrequency">
                    <el-input v-model="form.assessmentManagement.attendanceFrequency" type="text"
                              placeholder="请输入听课次数"></el-input>
                </el-form-item>
                <el-form-item label="班会次数" prop="assessmentManagement.classFrequency">
                    <el-input v-model="form.assessmentManagement.classFrequency" type="text"
                              placeholder="请输入班会次数"></el-input>
                </el-form-item>
                <el-form-item label="检查宿舍次数" prop="assessmentManagement.dormitoriesFrequency">
                    <el-input v-model="form.assessmentManagement.dormitoriesFrequency" type="text"
                              placeholder="请输入检查宿舍次数"></el-input>
                </el-form-item>
                <el-form-item label="参加活动次数" prop="assessmentManagement.activeFrequency">
                    <el-input v-model="form.assessmentManagement.activeFrequency" type="text"
                              placeholder="请输入参加活动次数"></el-input>
                </el-form-item>
                <el-form-item label="学期不及格人次" prop="assessmentStudy.failedAmount">
                    <el-input v-model="form.assessmentStudy.failedAmount" type="text"
                              placeholder="请输入学期不及格人次"></el-input>
                </el-form-item>
<!--                <el-form-item label="学期不及格百分比" prop="assessmentStudy.failedPercentage">-->
<!--                    <el-input v-model="form.assessmentStudy.failedPercentage" type="text"-->
<!--                              placeholder="请输入学期不及格百分比"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="四级通过人数" prop="assessmentStudy.gradeFourAmount">
                    <el-input v-model="form.assessmentStudy.gradeFourAmount" type="text"
                              placeholder="请输入四级通过人数"></el-input>
                </el-form-item>
<!--                <el-form-item label="四级通过百分比" prop="assessmentStudy.gradeFourPercentage">-->
<!--                    <el-input v-model="form.assessmentStudy.gradeFourPercentage" type="text"-->
<!--                              placeholder="请输入:"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="六级通过人数" prop="assessmentStudy.gradeSixAmount">
                    <el-input v-model="form.assessmentStudy.gradeSixAmount" type="text" placeholder="请输入:"></el-input>
                </el-form-item>
<!--                <el-form-item label="六级通过百分比" prop="assessmentStudy.gradeSixPercentage">-->
<!--                    <el-input v-model="form.assessmentStudy.gradeSixPercentage" type="text"-->
<!--                              placeholder="请输入:"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="一等奖学金人数" prop="assessmentStudy.firstScholarshipAmount">
                    <el-input v-model="form.assessmentStudy.firstScholarshipAmount" type="text"
                              placeholder="请输入:"></el-input>
                </el-form-item>
<!--                <el-form-item label="一等奖学金百分比" prop="assessmentStudy.firstScholarshipPercentage">-->
<!--                    <el-input v-model="form.assessmentStudy.firstScholarshipPercentage" type="text"-->
<!--                              placeholder="请输入:"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="二等奖学金人数" prop="assessmentStudy.secondScholarshipAmount">
                    <el-input v-model="form.assessmentStudy.secondScholarshipAmount" type="text"
                              placeholder="请输入:"></el-input>
                </el-form-item>
<!--                <el-form-item label="二等奖学金百分比" prop="assessmentStudy.secondScholarshipPercentage">-->
<!--                    <el-input v-model="form.assessmentStudy.secondScholarshipPercentage" type="text"-->
<!--                              placeholder="请输入:"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="三等奖学金人数" prop="assessmentStudy.thirdScholarshipAmount">
                    <el-input v-model="form.assessmentStudy.thirdScholarshipAmount" type="text"
                              placeholder="请输入:"></el-input>
                </el-form-item>
<!--                <el-form-item label="三等奖学金百分比" prop="assessmentStudy.thirdScholarshipPercentage">-->
<!--                    <el-input v-model="form.assessmentStudy.thirdScholarshipPercentage" type="text"-->
<!--                              placeholder="请输入:"></el-input>-->
<!--                </el-form-item>-->
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addVisible = false">取 消</el-button>
                    <el-button type="primary" @click="create">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import {ref, reactive} from "vue";
    import {ElMessage, ElMessageBox} from "element-plus";
    import axios from 'axios';
    import qs from 'qs';
    import moment from 'moment';

    export default {
        name: "dormitory",

        data() {
            return {
                pageParams: {
                    pageNum: 1, // 查询第一页
                    pageSize: 10 // 每页10条
                },
            };
        },
        setup() {
          const role = localStorage.getItem("ms_power");
            const query = reactive({
                name: "",
                tid: "",
                pageNum: 1,
                pageSize: 10
            });
            const tableData = ref([]);
            const pageTotal = ref(0);
            const result = ref();
            // 获取表格数据
            const getData = () => {
                axios.get('ks/assessment/selectAll', {
                    params: {
                        pageNum: query.pageNum,
                        pageSize: query.pageSize,
                        tid: query.tid,
                        name: query.name
                    }
                }).then(result => {
                    console.log('value:', result);
                    console.log("role = ", role);
                    tableData.value = result.data.result.list;
                    pageTotal.value = result.data.result.total || 0;
                    console.log('list:', result.data.result.list);
                    console.log('pageTotal:', result.data.result.total);
                }).catch(err => {
                    console.error(err)
                })


            };
            getData();

            // 查询操作
            const handleSearch = () => {
                query.pageNum = 1;
                getData();
            };
            // 分页导航
            const handlePageChange = (val) => {
                query.pageNum = val;
                getData();

            };
            //序号连续
            const indexAdd = (index) => {
                //  console.log("indexAdd:index=", index);
                const page = query.pageNum // 当前页码
                const pageSize = query.pageSize // 每页条数
                return index + 1 + (page - 1) * pageSize
            };
            const ellipsis = (value) => {
                // console.log("文本显示字数:",value);
                if (!value) return ''
                if (value.length > 25) {
                    return value.slice(0, 25) + '........';//0:下标,从第一个字开始显示,50:显示字数,多余用...代替
                }
                return value
            };
            //监听二次删除是否确定
            const isDelete = (id) => {
                ElMessageBox.confirm("确定要删除吗？", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: "warning"
                }).then((result) => {
                    //传入tid进行删除
                    const data = {
                        id: id,
                    }
                    console.log("删除-tid = ", id);
                    axios.post('ks/assessment/deleteById',
                        qs.stringify(data),
                        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
                    ).then((result) => {
                        if (result.data.success) {
                            ElMessage.success(`删除第 ${idx + 1} 行成功`);
                        } else {
                            ElMessage.error("删除失败");
                        }
                        getData();
                    });


                }).catch(() => {
                    ElMessage.info({message: '已取消删除'});
                    getData();
                });

            };
            //格式化时间
            const momentData = (form) => {
                form.time = moment(form.time).format('YYYY-MM-DD HH:mm:ss')
            }


            // 删除操作
            const handleDelete = (index, row) => {
                idx = index;
                Object.keys(form).forEach((item) => {
                    form[item] = row[item];
                });
                if(role !== "1") {
                  isDelete(form.id);
                } else {
                  ElMessage.error("啊偶、管理员无权限删除业务数据");
                  return;
                }
                tableData.value.splice(index, 1);

            };

            // 表格编辑时弹窗和保存
            const editVisible = ref(false);
            const addVisible = ref(false);

            const createForm = () => {
                addVisible.value = true;
               form.id="";
               form.tid="";
               form.name="";
               form.amount="";
               form.awards="";
               form.classNo="";
               form.remark="";
                Object.keys(form.assessmentManagement).forEach((item) => {
                     form.assessmentManagement[item] = "";
                });
                Object.keys(form.assessmentStudy).forEach((item) => {
                    form.assessmentStudy[item] = "";
                });

            };
            const assessmentManagement = reactive({
                id: "",
                refId: "",
                amount: "",
                type: "",
                attendanceFrequency: "",
                classFrequency: "",
                dormitoriesFrequency: "",
                activeFrequency: "",

            });

            const assessmentStudy = reactive({
                id: "",
                refId: "",
                failedAmount: "",
                failedPercentage: "",
                gradeFourAmount: "",
                gradeFourPercentage: "",
                gradeSixAmount: "",
                gradeSixPercentage: "",
                firstScholarshipAmount: "",
                firstScholarshipPercentage: "",
                secondScholarshipAmount: "",
                secondScholarshipPercentage: "",
                thirdScholarshipAmount: "",
                thirdScholarshipPercentage: "",
            });
            const defaultValue = {
                id: "",
                classNo: "",
                name: "",
                tid: "",
                amount: "",
                awards: "",
                remark: "",
                assessmentManagement: {
                    id: "",
                    tid: "",
                    amount: "",
                    type: "",
                    attendanceFrequency: "",
                    classFrequency: "",
                    dormitoriesFrequency: "",
                    activeFrequency: "",

                },
                assessmentStudy: {
                    id: "",
                    tid: "",
                    failedAmount: "",
                    failedPercentage: "",
                    gradeFourAmount: "",
                    gradeFourPercentage: "",
                    gradeSixAmount: "",
                    gradeSixPercentage: "",
                    firstScholarshipAmount: "",
                    firstScholarshipPercentage: "",
                    secondScholarshipAmount: "",
                    secondScholarshipPercentage: "",
                    thirdScholarshipAmount: "",
                    thirdScholarshipPercentage: "",
                },

            };
            const form = reactive(defaultValue);
            const validatePhone = (rule, value, callback) => {
                try {
                    if (!value) {
                        return new Error("请输入电话号码");
                    } else {
                        const reg = /^1[2|3|4|5|6|7|8|9][0-9]\d{8}$/;
                        const isPhone = reg.test(value);
                        value = Number(value); //转换为数字
                        if (typeof value === "tid" && !isNaN(value)) {//判断是否为数字
                            value = value.toString(); //转换成字符串
                            if (value.length < 0 || value.length > 12 || !isPhone) { //判断是否为11位手机号
                                callback(new Error("手机号码格式如:138xxxx8754"));
                            } else {
                                callback();
                            }
                        } else {
                            callback(new Error("手机号格式不对"));
                        }
                    }
                } catch (err) {
                    this.$message.error(err.message)
                }
            }
            //长度校验
            const checkLength = (rule, value, callback) => {
                if (value.length > 500) {
                    callback(new Error('描述不能超过500字符'));
                } else {
                    callback();
                }
            };
            //校验数字
            const checkNumber = (rule, value, callback) => {
                if (value || value === 0) {
                    const reg = /^[0-9]*$/;
                    const flag = reg.test(value);
                    console.log("flag-checkNumber:", flag)
                    console.log("checkNumber--value :", value)
                    if (!isNaN(value)) {        //判断是否为数字
                        value = value.toString(); //转换成字符串
                        if (value < 0 || !flag) {
                            callback(new Error("输入格式不对,请输入整数"));
                        } else {
                            callback();
                        }

                    } else {
                        callback(new Error("输入格式不对,请输入数字"));
                    }
                }
            }
            //校验百分比
            const checkRate = (rule, value, callback) => {
                if (value) {
                    const reg = /^(100|[1-9]?\d(\.\d\d?\d?)?)%$|0$/;
                    const flag = reg.test(value);
                    console.log("flag-checkRate:", flag)
                    if (flag) {
                        callback();
                    } else {
                        callback(new Error("输入格式不对,请输入百分比"));
                    }
                }
            }
            // 定义校验规则
            const editRules = reactive({
                name: [
                    {required: true, message: "用户名不能为空", trigger: 'blur'},
                    {validator: checkLength, trigger: "blur"}
                ],
                tid: [
                    {required: true, message: '教师编号不能为空', trigger: 'blur'},
                    {validator: checkLength, trigger: "blur"}
                ],
                classNo: [
                    {required: true, message: '所在班级不能为空', trigger: 'blur'},
                    {validator: checkLength, trigger: "blur"}
                ],
                amount: [
                    {required: true, message: '请输入学生人数...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                awards: [
                    {required: false, message: '请输入获奖情况....', trigger: 'blur'},
                    {validator: checkLength, trigger: "blur"}
                ],
                "assessmentManagement.amount": [
                    {required: false, message: '请输入处分人数...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentManagement.type": [
                    {required: true, message: '请选择', trigger: 'blur'}
                ],
                "assessmentManagement.attendanceFrequency": [
                    {required: true, message: '请输入听课次数...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentManagement.classFrequency": [
                    {required: true, message: '请输入班会次数...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentManagement.dormitoriesFrequency": [
                    {required: true, message: '请输入检查宿舍次数...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentManagement.activeFrequency": [
                    {required: true, message: '请输入参加活动次数...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.failedAmount": [
                    {required: true, message: '请输入学期不及格人次...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.failedPercentage": [
                    {required: true, message: '请输入学期不及格百分比...', trigger: 'blur'},
                    {validator: checkRate, trigger: "blur"}
                ],
                "assessmentStudy.gradeFourAmount": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.gradeFourPercentage": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkRate, trigger: "blur"}
                ],
                "assessmentStudy.gradeSixAmount": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.gradeSixPercentage": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkRate, trigger: "blur"}
                ],
                "assessmentStudy.firstScholarshipAmount": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.firstScholarshipPercentage": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkRate, trigger: "blur"}
                ],
                "assessmentStudy.secondScholarshipAmount": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.secondScholarshipPercentage": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkRate, trigger: "blur"}
                ],
                "assessmentStudy.thirdScholarshipAmount": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkNumber, trigger: "blur"}
                ],
                "assessmentStudy.thirdScholarshipPercentage": [
                    {required: true, message: '请输入...', trigger: 'blur'},
                    {validator: checkRate, trigger: "blur"}
                ],
                remark: [
                    {required: false, message: '请输入....', trigger: 'blur'},
                    {validator: checkLength, trigger: "blur"}
                ]
            })

            let idx = -1;
            const handleEdit = (index, row) => {
                idx = index;
                Object.keys(form).forEach((item) => {
                    form[item] = row[item];
                });
                if(role !== "1") {
                  console.log('assessmentManagement', form.assessmentManagement)
                  console.log('row', row)
                  editVisible.value = true;
                } else {
                  ElMessage.error("啊偶、管理员无权限更新业务数据");
                  return;
                }

                getData();
            };
            //todo
            const formRef = ref(null);
            const saveEdit = () => {

                //表单校验
                console.log("form为:", form)
                console.log("编辑--formRef--:", formRef.value);
                formRef.value.validate((valid) => {
                    if (valid) {
                        console.log("编辑--表单校验222:", form);
                        axios.post('ks/assessment/update', form)
                            .then((result) => {
                                    editVisible.value = false;
                                    if (result.data.success) {
                                        ElMessage.success(`修改第 ${idx + 1} 行成功`);
                                    } else {
                                        ElMessage.error(result.data.message + ':数据格式不正确');
                                    }
                                getData();
                                }
                            )
                    } else {
                        ElMessage.error("提交失败");
                        getData();
                    }
                });
                Object.keys(form).forEach((item) => {
                    tableData.value[idx][item] = form[item];
                });

            };

            const create = () => {
                formRef.value.validate((valid) => {
                    if (valid) {
                        console.log("新增--表单校验:", form);
                        momentData(form);
                        console.log("新增--表单时间:", form.time);
                        axios.post('ks/assessment/save', form)
                            .then((result) => {
                                    addVisible.value = false;
                                    if (result.data.success) {
                                        ElMessage.success(`添加成功`);

                                    } else {
                                        ElMessage.error(result.data.message);
                                    }
                                getData();
                                }
                            )
                    } else {
                        ElMessage.error("提交失败");
                        getData();
                    }
                });
            };


            return {
                query,
                tableData,
                pageTotal,
                editVisible,
                form,
                editRules,
                addVisible,
                formRef,
                result,
                assessmentManagement,
                assessmentStudy,
                defaultValue,
                role,
                checkRate,
                checkNumber,
                ellipsis,
                momentData,
                checkLength,
                isDelete,
                validatePhone,
                createForm,
                handleSearch,
                handlePageChange,
                handleDelete,
                handleEdit,
                saveEdit,
                create,
                indexAdd
            };
        },
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-right {
        margin-bottom: 20px;
        float: right;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
